package net.orange.framework.controller;

import java.util.ArrayList;
import java.util.Collection;
import java.util.GregorianCalendar;

import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
import javax.faces.model.DataModel;
import javax.faces.model.ListDataModel;

import net.orange.architecture.db.Condition;
import net.orange.architecture.db.Conditions;
import net.orange.architecture.process.IBaseProcess;
import net.orange.architecture.process.ProcessFactory;
import net.orange.architecture.web.BaseController;
import net.orange.architecture.web.Web;
import net.orange.framework.model.Calendar;
import net.orange.framework.model.MonthCalendar;

/**
 * The calendar managed bean using in JSF MVC modal. it is include the web page
 * content and control the action and page forward direction in the web site.
 */
@ManagedBean(name="calendar")
@SessionScoped
public class CalendarController extends BaseController {

	/**
	 * The year which want to search.
	 */
	private int i_year;

	/**
	 * The month which want to search.
	 */
	private int i_month;

	/**
	 * The list data model for calendar.
	 */
	DataModel calendarDatas = new ListDataModel();

	/*
	 * (non-Javadoc)
	 * 
	 * @see net.orange.architecture.web.BaseController#getClazz()
	 */
	protected Class getClazz() {
		return Calendar.class;
	}

	/**
	 * @return Returns the i_month.
	 */
	public int getI_month() {
		if (i_month == 0) {
			java.util.Calendar calendar = java.util.Calendar.getInstance();
			calendar.setTimeInMillis(System.currentTimeMillis());
			return calendar.get(java.util.Calendar.MONTH) + 1;
		} else {
			return i_month;
		}
	}

	/**
	 * @param i_month
	 *            The i_month to set.
	 */
	public void setI_month(int i_month) {
		this.i_month = i_month;
	}

	/**
	 * @return Returns the i_year.
	 */
	public int getI_year() {
		if (i_year == 0) {
			java.util.Calendar calendar = java.util.Calendar.getInstance();
			calendar.setTimeInMillis(System.currentTimeMillis());
			return calendar.get(java.util.Calendar.YEAR);
		} else {
			return i_year;
		}
	}

	/**
	 * @param i_year
	 *            The i_year to set.
	 */
	public void setI_year(int i_year) {
		this.i_year = i_year;
	}

	/**
	 * Retrieve the monthly calendar format list modal.
	 * 
	 * @return return the current month calendar.
	 */
	public DataModel getCurrentMonthCalendar() throws Exception {

		IBaseProcess process = ProcessFactory.load(getClazz());

		Conditions cnds = new Conditions();
		cnds.add(new Condition("year", getI_year()));
		cnds.add(new Condition("month", getI_month()));

		Collection day = process.listAll(cnds);

		Collection<MonthCalendar> calendar = new ArrayList<MonthCalendar>();

		MonthCalendar row = new MonthCalendar();
		calendar.add(row);

		for (Object b : day.toArray()) {
			GregorianCalendar grc = new GregorianCalendar();
			grc.setTime(((Calendar) b).getDate());

			int weekday = grc.get(GregorianCalendar.DAY_OF_WEEK) - 1;

			if (weekday == 0)
				row.setSunday((Calendar) b);
			if (weekday == 1)
				row.setMonday((Calendar) b);
			if (weekday == 2)
				row.setTuesday((Calendar) b);
			if (weekday == 3)
				row.setWednesday((Calendar) b);
			if (weekday == 4)
				row.setThursday((Calendar) b);
			if (weekday == 5)
				row.setFriday((Calendar) b);
			if (weekday == 6) {
				row.setSaturday((Calendar) b);
				row = new MonthCalendar();
				calendar.add(row);

			}

		}

		calendarDatas.setWrappedData(calendar);

		return calendarDatas;
	}

	/**
	 * Open the day which is Sunday.
	 * 
	 * @return "open" to forward to edit page.
	 * @throws Exception
	 */
	public String openSunday() throws Exception {
		content = ((MonthCalendar) calendarDatas.getRowData()).getSunday();
		this.editMode = false;

		return Web.JSF_NAVIGATION_OPEN;
	}

	/**
	 * Open the day which is Monday.
	 * 
	 * @return "open" to forward to edit page.
	 * @throws Exception
	 */
	public String openMonday() throws Exception {
		content = ((MonthCalendar) calendarDatas.getRowData()).getMonday();
		this.editMode = false;

		return Web.JSF_NAVIGATION_OPEN;
	}

	/**
	 * Open the day which is Tuesday.
	 * 
	 * @return "open" to forward to edit page.
	 * @throws Exception
	 */
	public String openTuesday() throws Exception {
		content = ((MonthCalendar) calendarDatas.getRowData()).getTuesday();
		this.editMode = false;

		return Web.JSF_NAVIGATION_OPEN;
	}

	/**
	 * Open the day which is Wednesday.
	 * 
	 * @return "open" to forward to edit page.
	 * @throws Exception
	 */
	public String openWednesday() throws Exception {
		content = ((MonthCalendar) calendarDatas.getRowData()).getWednesday();
		this.editMode = false;

		return Web.JSF_NAVIGATION_OPEN;
	}

	/**
	 * Open the day which is Thursday.
	 * 
	 * @return "open" to forward to edit page.
	 * @throws Exception
	 */
	public String openThursday() throws Exception {
		content = ((MonthCalendar) calendarDatas.getRowData()).getThursday();
		this.editMode = false;

		return Web.JSF_NAVIGATION_OPEN;
	}

	/**
	 * Open the day which is Friday.
	 * 
	 * @return "open" to forward to edit page.
	 * @throws Exception
	 */
	public String openFriday() throws Exception {
		content = ((MonthCalendar) calendarDatas.getRowData()).getFriday();
		this.editMode = false;

		return Web.JSF_NAVIGATION_OPEN;
	}

	/**
	 * Open the day which is Saturday.
	 * 
	 * @return "open" to forward to edit page.
	 * @throws Exception
	 */
	public String openSaturday() throws Exception {
		content = ((MonthCalendar) calendarDatas.getRowData()).getMonday();
		this.editMode = false;

		return Web.JSF_NAVIGATION_OPEN;
	}

}
